import {configOptions} from '../config'
import JSEncrypt from '../utils/jsencrypt'

export async function httpRequest(url,opts){
    const {options} = configOptions
    opts.body = opts.body && JSON.stringify(opts.body)
    url = options.url+url
    opts.headers = Object.assign({
      'Content-Type':'application/json',
      'token':options.token || ''
    },opts.headers||{})
    if(window && fetch){
      return fetch(url,opts).then(response => response.json())
    }else{
      return new Promise((resolve,reject)=>{
        wx.request({
          url: url,
          method: opts.method || "get",
          header: opts.headers,
          data:opts.body,
          success:function(res){
            resolve(res.data)
          },
          fail:function(error){
            reject(error)
          }
        })
      })
    }
    
}    

export const public_key = `-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAyzPfS5hSIDLLndLkjMZI
QAbiw7k5tSrqlUDSx2WxpzDyJNqI7LdM3/RQO5LyIRiidLJbq19B1qIfCvPEwin6
qy49uEIUBhp4wNdWv/XFWm13xawiMjaKUGtva8R65Fa6ZLyoaBhYSvpehnmzqz6V
fXg5v7jTh8kocL4Zs7mdZXyTW8gxox8J8BwFLploiGs22Lo/qba2d/iudEC1O90l
EikqByvBybXl6d6msSpdlvwTF0FkBj1DYbEcmbPG8PPVCAfbDSnQ1AqEvRQoQlw1
scYEBnbSk9ihwWcsyNtOOSl8laBFvxF3l7lm3HIxfbgz/CHzFK7L1IiaWPtgnBe7
/wIDAQAB
-----END PUBLIC KEY-----
`
export const private_key = `-----BEGIN PRIVATE KEY-----
MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQDLM99LmFIgMsud
0uSMxkhABuLDuTm1KuqVQNLHZbGnMPIk2ojst0zf9FA7kvIhGKJ0slurX0HWoh8K
88TCKfqrLj24QhQGGnjA11a/9cVabXfFrCIyNopQa29rxHrkVrpkvKhoGFhK+l6G
ebOrPpV9eDm/uNOHyShwvhmzuZ1lfJNbyDGjHwnwHAUumWiIazbYuj+ptrZ3+K50
QLU73SUSKSoHK8HJteXp3qaxKl2W/BMXQWQGPU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qOLhtlWb1FfKqq3M8C40aAZXXJ2EtHVgPX3nwYuqSVL+T9SLo
bTy3B0kKolGn1kBEJZYx7yfRVZgv5PLCAofkH/ChAoGBAOcx6/Ahk4x6OJG6BiJp
ncAkIoPwJ9+c6cNkmp1Y2wBdhmfvPgHqGLaF8YZK7Iw2lCviLeSJg8m1W4ia/pzc
SZG8H9BHflIZt8zNna2Nkn88wQ2oSeWkGBQj2X5ktpvMS/rpHgdDwt/fhN6upH0j
PC/wcGUT+e26VXPOkPG8qpc2
-----END PRIVATE KEY-----
`
export function RSAencript(data){
  const encrypt = new JSEncrypt()
  encrypt.setPublicKey(public_key)
  let result = encrypt.encrypt(data)
  return result
}
export function RSAdecript(data){
  const decrypt = new JSEncrypt()
  decrypt.setPrivateKey(private_key)
  let result = decrypt.decrypt(data)
  return result
}